﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model.Group;

namespace YouHP.Pages.Group
{
    public class ReplyEdit : YouHP.Pages.Group.GroupBasePage
    {
        protected TopicInfo TopicData { get; set; }
        protected ReplyInfo ReplyData { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.SetTitle("修改回复");

            int tid = RequestTools.GetInt("tid", 0);
            if (tid <= 0)
            {
                this.AddErrLine("无效的参数 tid");
                return;
            }
            TopicData = TopicBLL.Instance.GetTopicInfo(tid, gid);
            if (TopicData.Id <= 0)
            {
                this.AddErrLine("帖子不存在或已被删除");
                return;
            }

            int rid = RequestTools.GetInt("rid", 0);
            if (rid <= 0)
            {
                this.AddErrLine("无效的参数 rid");
                return;
            }

            ReplyData = ReplyBLL.Instance.GetReplyInfo(rid);

            if (ReplyData.Id <= 0)
            {
                this.AddErrLine("该回复不存在");
                return;
            }
        }

        protected void HandlePost()
        {
            ReplyInfo info = ValidateForm();
            if (info == null) return;
            bool succ = ReplyBLL.Instance.UpdateReply(info);
            if (succ == false)
            {
                this.AddErrLine("修改回复失败");
                return;
            }
            else
            {
                string nexturl = "/Group/Topic.aspx?gid=" + GroupData.Id + "&tid=" + ReplyData.TopicId;
                this.Redirect(nexturl);
            }
        }

        protected void HandleGet()
        {
           
        }

        protected ReplyInfo ValidateForm()
        {
            ReplyData.Body = RequestTools.GetFormString("tbxContent").Trim();
            if (ReplyData.Body.Length == 0)
            {
                this.AddErrLine("回复长度不能为空！");
                return null;
            }
            else if (ReplyData.Body.Length > Config.ReplyMaxSize)
            {
                this.AddErrLine("回复长度不能大于 " + Config.ReplyMaxSize + "字符！");
                return null;
            }
            ReplyData.UpdateTime = DateTime.Now;

            return ReplyData;
        }
    }
}